package com.yunhe.policyfulfillment.policyful.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.policyfulfillment.authority.domain.PolicyOrg;
import com.yunhe.policyfulfillment.authority.domain.PolicyUser;
import com.yunhe.policyfulfillment.authority.repository.IPolicyOrgRepository;
import com.yunhe.policyfulfillment.authority.service.IPolicyOrgService;
import com.yunhe.policyfulfillment.authority.service.IPolicyUserService;
import com.yunhe.policyfulfillment.constant.PolicyFulfillmentConstant;
import com.yunhe.policyfulfillment.policyful.domain.PolicyInfoNews;
import com.yunhe.policyfulfillment.policyful.domain.PolicyResult;
import com.yunhe.policyfulfillment.policyful.model.PolicyResultQueryCondition;
import com.yunhe.policyfulfillment.policyful.model.PolicyResultVo;
import com.yunhe.policyfulfillment.policyful.model.PublishStatus;
import com.yunhe.policyfulfillment.policyful.repository.PolicyResultRepository;
import com.yunhe.policyfulfillment.policyful.service.PolicyResultService;
import com.yunhe.policyfulfillment.util.MyHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yunhe.common.util.CollectionUtil;

import java.util.*;

/**
 * @description 结果公示表
 * @author misty
 * @date 2021-09-17
 */
@Service
public class PolicyResultServiceImpl implements PolicyResultService {

    @Autowired
    private PolicyResultRepository policyResultRepository;

    @Autowired
    private MyHeaderUtil myHeaderUtil;

    @Autowired
    private IPolicyUserService policyUserService;

    @Autowired
    private IPolicyOrgService policyOrgService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int createPolicyResult (PolicyResult policyResult) throws ArgumentErrorException {
        PolicyUser headerUser = myHeaderUtil.getHeaderUser();
        if (headerUser == null) {
            throw new ArgumentErrorException("登录信息已失效,请重新登录!");
        }
        policyResult.setPublishPerson(headerUser.getId());
        //policyResult.setPublishOrg(headerUser.getOrgId());
        processBeforeSave(policyResult);
        return policyResultRepository.createPolicyResultSelective(policyResult);
    }

    private void processBeforeSave(PolicyResult policyResult) {
        long currentTimeMillis = System.currentTimeMillis();
        if (policyResult.getPublishTime() == null) {
            policyResult.setIsOnline(PolicyFulfillmentConstant.Online_Type.Online.value());
            policyResult.setPublishTime(new Date());
        } else {
            if (policyResult.getPublishTime().getTime() <= currentTimeMillis) {
                policyResult.setIsOnline(PolicyFulfillmentConstant.Online_Type.Online.value());
            }
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePolicyResult(int id) {
        return policyResultRepository.deletePolicyResultByPrimaryKey(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePolicyResult(PolicyResult policyResult) {
        policyResult.setUpdatePerson(myHeaderUtil.getHeaderUserId());
        long time = policyResult.getPublishTime().getTime();
        if (time <= System.currentTimeMillis()){
            policyResult.setIsOnline(PolicyFulfillmentConstant.Online_Type.Online.value());
        }else {
            policyResult.setIsOnline(PolicyFulfillmentConstant.Online_Type.Offline.value());
        }
        return policyResultRepository.updatePolicyResultByPrimaryKeySelective(policyResult);
    }


    @Override
    public PolicyResult findPolicyResultById(int id) {
        return policyResultRepository.searchPolicyResultByPrimaryKey(id);
    }

    @Override
    public PageResult findPolicyResultByCondition(PolicyResultQueryCondition policyResultQueryCondition) {
        PageResult result = null;
        PageHelper.startPage(policyResultQueryCondition.getPage(), policyResultQueryCondition.getSize());
        List<PolicyResultVo> policyResultVos = policyResultRepository.findPolicyResultByCondition(policyResultQueryCondition);
        if (!CollectionUtil.isEmpty(policyResultVos)) {
            policyResultVos.forEach(policyResultVo -> {
                if (policyResultVo.getUpdatePerson() != null) {
                    PolicyUser updatePerson = policyUserService.findPolicyUserById(policyResultVo.getUpdatePerson());
                    if (updatePerson != null) {
                        policyResultVo.setUpdatePersonName(updatePerson.getUserName());
                    }
                }
                PolicyUser publishPerson = policyUserService.findPolicyUserById(policyResultVo.getPublishPerson());
                if (publishPerson != null) {
                    policyResultVo.setPublishPersonName(publishPerson.getUserName());
                }
                PolicyOrg publishOrg = policyOrgService.findPolicyOrgByOrgId(policyResultVo.getPublishOrg());
                if (publishOrg != null) {
                    policyResultVo.setPublishOrgName(publishOrg.getOrgName());
                }

            });
            PageInfo<PolicyResultVo> pageInfo = new PageInfo<>(policyResultVos);
            result = new PageResult(policyResultQueryCondition.getPage(), policyResultQueryCondition.getSize(), (int) pageInfo.getTotal(), policyResultVos);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void publishTimedPolicyResult() {
        policyResultRepository.publishTimedPolicyResult();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setPublishStatus(PublishStatus publishStatus) {
        List<Long> ids = publishStatus.getIds();
        if(!CollectionUtil.isEmpty(ids)){
            for (Long id : ids) {
                PolicyResult policyResult = new PolicyResult();
                policyResult.setId(id);
                policyResult.setIsOnline(publishStatus.getPublishStatus());
                if(PolicyFulfillmentConstant.Online_Type.Online.value().equals(publishStatus.getPublishStatus())){
                    policyResult.setPublishTime(new Date());
                }
                policyResultRepository.updatePolicyResultByPrimaryKeySelective(policyResult);
            }
        }
    }

    @Override
    public PageResult getPolicyResultByCondition(PolicyResultQueryCondition policyResultQueryCondition) {
        PageResult result = null;
        policyResultQueryCondition.setIsOnline(PolicyFulfillmentConstant.Online_Type.Online.value());
        PageHelper.startPage(policyResultQueryCondition.getPage(), policyResultQueryCondition.getSize());
        List<PolicyResultVo> policyResultVos = policyResultRepository.findPolicyResultByCondition(policyResultQueryCondition);
        if (!CollectionUtil.isEmpty(policyResultVos)) {
            policyResultVos.forEach(policyResultVo -> {
                if (policyResultVo.getUpdatePerson() != null) {
                    PolicyUser updatePerson = policyUserService.findPolicyUserById(policyResultVo.getUpdatePerson());
                    if (updatePerson != null) {
                        policyResultVo.setUpdatePersonName(updatePerson.getUserName());
                    }
                }
                PolicyUser publishPerson = policyUserService.findPolicyUserById(policyResultVo.getPublishPerson());
                if (publishPerson != null) {
                    policyResultVo.setPublishPersonName(publishPerson.getUserName());
                }
                PolicyOrg publishOrg = policyOrgService.findPolicyOrgByOrgId(policyResultVo.getPublishOrg());
                if (publishOrg != null) {
                    policyResultVo.setPublishOrgName(publishOrg.getOrgName());
                }

            });
            PageInfo<PolicyResultVo> pageInfo = new PageInfo<>(policyResultVos);
            result = new PageResult(policyResultQueryCondition.getPage(), policyResultQueryCondition.getSize(), (int) pageInfo.getTotal(), policyResultVos);
        }
        return result;
    }


}